/**
 *系统相关自动补全功能抽取，导入此文件需先导入autocomplete框架和jquery
 */

/**
 * 大转盘奖品
 * @param nodeId
 * @param callBack
 */
var selectDaZhuanPanPrize = function (contextPath,nodeSelect,callBack) {
    $.ajax({
        url: contextPath + '/shop/selectDaZhuanPanPrizeToSelected',
        success: function (data) {
            if (data.success) {
                var countries = data.data
                $(nodeSelect).autocomplete({
                    lookup: countries,
                    onSelect: function (suggestion) {
                        ////console.log('You selected: ' + suggestion.value + ', ' + suggestion.data);
                        if (typeof callBack === 'function') {
                            callBack(suggestion.value,suggestion.data)
                        }else {
                            if (callBack) console.error('callBack is not a function');
                        }
                    },
                    //width: 200,
                    minChars: 0,
                });
            }
        }
    })
};

/**
 * 经营类型
 */
var selectCityTypeToSelected = function (contextPath,nodeSelect,callBack) {
    $.ajax({
        url: contextPath + '/checking/selectCityTypeToSelected',
        success: function (data) {
            if (data.success) {
                var countries = data.data
                $(nodeSelect).autocomplete({
                    lookup: countries,
                    onSelect: function (suggestion) {
                        ////console.log('You selected: ' + suggestion.value + ', ' + suggestion.data);
                        if (typeof callBack === 'function') {
                            callBack(suggestion.value,suggestion.data)
                        }else {
                            if (callBack) console.error('callBack is not a function');
                        }
                    },
                    //width: 200,
                    minChars: 0,
                });
            }
        }
    })
};

/**
 * 档位名称
 */
var selectOrderLevel = function (contextPath,nodeSelect,callBack) {
    $.ajax({
        url: contextPath + '/ktCommon/getZoneListByPage',
        success: function (data) {
            if (data.success) {
                var countries = data.data
                $(nodeSelect).autocomplete({
                    lookup: countries,
                    onSelect: function (suggestion) {
                        ////console.log('You selected: ' + suggestion.value + ', ' + suggestion.data);
                        if (typeof callBack === 'function') {
                            callBack(suggestion.value,suggestion.data)
                        }else {
                            if (callBack) console.error('callBack is not a function');
                        }
                    },
                    //width: 200,
                    minChars: 0,
                });
            }
        }
    })
};
/**
 * 物料来源
 * @param nodeSelect
 * @param callBack
 */
var selectSource = function (nodeSelect, callBack) {
    $(nodeSelect).autocomplete({
        lookup: [{data:'盘县', value:'盘县'},{data:'六枝', value:'六枝'},{data:'六盘水市局', value:'六盘水市局'},{data:'水城县局', value:'水城县局'},{data:'钟山县局', value:'钟山县局'}],
        onSelect: function (suggestion) {
            //alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
            if (typeof callBack === 'function') {
                callBack(suggestion.value,suggestion.data)
            }else {
                if (callBack) console.error('callBack is not a function');
            }
        },
        //width: 200,
        minChars: 0,
    });
};

var selectCustName = function (contextPath,nodeSelect,callBack,params) {
    $.ajax({
        url: contextPath + '/ktCommon/getZoneListByPage',
        type : 'post',
        data : params,
        success: function (data) {
            if (data.success) {
                var countries = data.data
                $(nodeSelect).autocomplete({
                    lookup: countries,
                    onSelect: function (suggestion) {
                        // console.log('You selected: ' + suggestion.value + ', ' + suggestion.data);
                        if (typeof callBack === 'function') {
                            callBack(suggestion.value,suggestion.data)
                        }else {
                            if (callBack) console.error('callBack is not a function');
                        }
                    },
                    minChars: 0,
                });
            }
        }
    })
};

var selectShopName = function (contextPath,nodeSelect,callBack,params) {
    $.ajax({
        url: contextPath + '/ktCommon/getZoneListByPage',
        type : 'post',
        data : params,
        success: function (data) {
            if (data.success) {
                var countries = data.recordList
                $(nodeSelect).autocomplete({
                    lookup: countries,
                    onSelect: function (suggestion) {
                        // alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
                        if (typeof callBack === 'function') {
                            callBack(suggestion.value,suggestion.data)
                        }else {
                            if (callBack) console.error('callBack is not a function');
                        }
                    },
                    minChars: 0,
                });
            }
        }
    })
}

var selectEmpName = function (contextPath,nodeSelect,callBack,params) {
    $.ajax({
        url: contextPath + '/ktCommon/getZoneListByPage',
        type : 'post',
        data : params,
        success: function (data) {
            if (data.success) {
                var countries = data.recordList
                $(nodeSelect).autocomplete({
                    lookup: countries,
                    onSelect: function (suggestion) {
                        if (typeof callBack === 'function') {
                            callBack(suggestion.value,suggestion.data)
                        }else {
                            if (callBack) console.error('callBack is not a function');
                        }
                    },
                    width: 200,
                    minChars: 0,
                });
            }
        }
    })
};

var selectChannelName = function (contextPath,nodeSelect,callBack,params) {
    $.ajax({
        url: contextPath + '/ktCommon/getZoneListByPage',
        type : 'post',
        data : params,
        success: function (data) {
            var countries = [];
            var dataArray = data.recordList;
            // console.log(dataArray)
            for (var i=0;i<dataArray.length;i++){
                var list = {};
                list.data = dataArray[i].id;
                list.value = dataArray[i].zoneName;
                if (dataArray[i].zoneName){
                    countries.push(list);
                }

            }
            var dataList = countries;
            // console.log(countries)
            $(nodeSelect).autocomplete({
                lookup: countries,
                onSelect: function (suggestion) {
                    // co ('You selected: ' + suggestion.value + ', ' + suggestion.data);
                    if (typeof callBack === 'function') {
                        callBack(suggestion.value,suggestion.data)
                    }else {
                        if (callBack) console.error('callBack is not a function');
                    }
                },
                width: 200,
                minChars: 0,
            });
        }
    })
};

var selectQuXianName = function (contextPath,nodeSelect,callBack,params) {
    $.ajax({
        url: contextPath + '/ktCommon/getZoneListByPage',
        type : 'post',
        data : params,
        success: function (data) {
            var countries = [];
            var dataArray = data.recordList;
            // console.log(dataArray)
            for (var i=0;i<dataArray.length;i++){
                var list = {};
                list.data = dataArray[i].id;
                list.value = dataArray[i].zoneName;
                if (dataArray[i].zoneName){
                    countries.push(list);
                }

            }
            var dataList = countries;
            // console.log(countries)
            $(nodeSelect).autocomplete({
                lookup: countries,
                onSelect: function (suggestion) {
                    // co ('You selected: ' + suggestion.value + ', ' + suggestion.data);
                    if (typeof callBack === 'function') {
                        callBack(suggestion.value,suggestion.data)
                    }else {
                        if (callBack) console.error('callBack is not a function');
                    }
                },
                width: 200,
                minChars: 0,
            });
        }
    })
};

var selectCityName = function (contextPath,nodeSelect, callBack, queryModel) {

    $.ajax({
        url: contextPath + '/ktCommon/getZoneListByPage',
        type : 'post',
        data : queryModel,
        success: function (data) {
            var countries = [];
            var dataArray = data.recordList;
            // console.log(dataArray)
            for (var i=0;i<dataArray.length;i++){
                var list = {};
                list.data = dataArray[i].id;
                list.value = dataArray[i].zoneName;
                if (dataArray[i].zoneName){
                    countries.push(list);
                }

            }
            var dataList = countries;

            $(nodeSelect).autocomplete({
                lookup: dataList,
                onSelect: function (suggestion) {
                    // alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
                    if (typeof callBack === 'function') {
                        callBack(suggestion.value,suggestion.data)
                    }else {
                        if (callBack) console.error('callBack is not a function');
                    }
                },
                //width: 200,
                minChars: 0,
            });
        }
    })
};

//供应商
var selectSupplierName = function (contextPath, nodeSelect, callBack) {
    $.ajax({
        url: contextPath + '/ktPurchase/getSupplierListByPage',
        type : 'post',
        data : queryModel,
        success: function (data) {
            var countries = [];
            var dataArray = data.recordList;
            for (var i=0;i<dataArray.length;i++){
                var list = {};
                list.data = dataArray[i].id;
                list.value = dataArray[i].supplierName;
                if (!dataArray[i].supplierName){

                }

            }
            var dataList = countries;

            // countries.push(list);
            // console.log(dataList)

            $(nodeSelect).autocomplete({
                lookup: dataList,
                onSelect: function (suggestion) {
                    // alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
                    if (typeof callBack === 'function') {
                        callBack(suggestion.value,suggestion.data)
                    }else {
                        if (callBack) console.error('callBack is not a function');
                    }
                },
                //width: 200,
                minChars: 0,
            });
        }
    })
};

var organization_tree = function (contextPath, cityNode, quXianNode, channelNode, empNode, custNode, shopNode, supplierName) {


    //客户名称
    var custSelect = function (value,data) {
        if (custNode) {
            selectCustName(contextPath,custNode, null ,{empName : value});
        }
    };
    if (custNode) {
        custSelect('','');
    }

    //店铺名称
    var shopSelect = function (value,data) {
        if (shopNode) {
            selectShopName(contextPath, shopNode, null, {empName: value});
        }
    };
    if (shopNode) {
        shopSelect('','');
    }

    //县级
    var empSelect = function (value,data) {
        queryModel.parentId = data
        if (empNode) {
            selectEmpName(contextPath, empNode, function (value, data) {
                custSelect(value,data);
                shopSelect(value,data);
            }, {channelName: value});
        }
    };
    if (empNode) {
        empSelect('','');
    }

    //市级
    var channelSelect = function (value,data) {
        // console.log(data)
        queryModel.parentId = data
        var filters = {
            "rules": [
                {
                    "field": "parentId",
                    "data": data
                }
            ]
        };
        queryModel.filters = JSON.stringify(filters);
        if (channelNode) {
            selectChannelName(contextPath, channelNode, empSelect, queryModel);
        }
    };
    if (channelNode) {
        channelSelect('','');
    }

    //省级
    var quXianSelect = function (value,data) {
        console.log(data)
        queryModel.parentId = data
        var filters = {
            "rules": [
                {
                    "field": "parentId",
                    "data": data
                }
            ]
        };
        queryModel.filters = JSON.stringify(filters);
        if (quXianNode) {
            selectQuXianName(contextPath, quXianNode,channelSelect , queryModel);
        }
    };


    if (quXianNode) {
        quXianSelect('','');
    }

    //主区域
    if (cityNode) {
        var filters = {
            "rules": [
                {
                    "field": "parentId",
                    "data": '0'
                }
            ]
        };
        queryModel.filters = JSON.stringify(filters);
        queryModel.rows = '100';
        selectCityName(contextPath,cityNode,quXianSelect, queryModel);
    }

    //供应商
    if (supplierName){
        selectSupplierName(contextPath,supplierName,quXianSelect)
    }
};

var selectSubProdType = function (contextPath,nodeSelect,callBack,params) {
    $.ajax({
        url: contextPath + '/ktProd/getSubProdType',
        type : 'post',
        data : params,
        success: function (data) {
            if (data.success) {
                var countries = data.data
                console.log(countries)
                $(nodeSelect).autocomplete({
                    lookup: countries,
                    onSelect: function (suggestion) {
                        co ('You selected: ' + suggestion.value + ', ' + suggestion.data);
                        if (typeof callBack === 'function') {
                            callBack(suggestion.value,suggestion.data)
                        }else {
                            if (callBack) console.error('callBack is not a function');
                        }
                    },
                    width: 200,
                    minChars: 0,
                });
            }
        }
    })
};

organization_getSubProdType = function(contextPath, yanType){
    console.log("yanType: "+yanType+"    "+new Date())
    //联动
    var getSubProdType = function (value,data) {
        if (yanType) {
            console.log("yanType: "+yanType+"    "+new Date())
            selectSubProdType(contextPath,yanType, null, {parentId: 0});
        }
    };

    if (yanType) {
        getSubProdType('','');
    }
};





/******** 商品类型自动补全代码  开始 *******/

//商品大类
var selectPordList = function (contextPath,nodeSelect, queryModel) {
    $.ajax({
        url: contextPath + '/ktProd/getSubProdType',
        type : 'post',
        data : queryModel,
        success: function (data) {
            console.log(data)
            var countries = [];
            var dataArray = data;
            for (var i=0;i<dataArray.length;i++){
                var list = {};
                list.data = dataArray[i].typeId;
                list.value = dataArray[i].typeName;
                if (!dataArray[i].typeName){
                    countries.push(list)
                }

            }
            var dataList = countries;

            // countries.push(list);
            console.log(dataList)

            $(nodeSelect).autocomplete({
                lookup: dataList,
                onSelect: function (suggestion) {
                    // alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
                    if (typeof callBack === 'function') {
                        callBack(suggestion.value,suggestion.data)
                    }else {
                        if (callBack) console.error('callBack is not a function');
                    }
                },
                //width: 200,
                minChars: 0,
            });
        }
    })
}
var organization_pord_tree = function (contextPath, prodInput1){

    if(prodInput1){
        //商品类
        var filters = {
            "rules": [
                {
                    "field": "parentId",
                    "data": '0'
                }
            ]
        };
        queryModel.filters = JSON.stringify(filters);
        queryModel.rows = '100';
        selectPordList(contextPath,prodInput1, queryModel);
    }
}

/******** 商品类型自动补全代码  结束 *******/